/**
 * Created by MADinow on 25.10.2014.
 */

/* -----------------------------------------------------------
                    MAIN MODULE
 ----------------------------------------------------------- */

define(['settings/variables', 'ui/madGenerator', 'ui/loading-ui', 'ui/mainContent-ui',
        'ui/pageDimmer-ui', 'ui/menu-ui', 'events/menu-events',
        'events/pageDimmer-events', 'events/user-events', 'jquery'],
    function (Variables, Genny, Loading, MainContent, PageDimmer, Menu, MenuEvents,
        PageDimmerEvents, UserEvents) {

        var _body = null ;
        var _mainPage = null ;

        /**
         * load all CSS files
         */
        function _initCSS() {
            Genny.createHeaderLink('text/css', Variables.cssPath + 'normalize.css', 'styleSheet');
            Genny.createHeaderLink('text/css', Variables.cssPath + 'font-awesome.min.css', 'styleSheet');
            Genny.createHeaderLink('text/css', Variables.cssPath + 'init.css', 'styleSheet');
        }

        /**
         * loads the page's favicon
         * @private
         */
        function _initTitleAndIcon() {
            Genny.createPageTitle(Variables.pageTitle);
            Genny.createHeaderLink('image/x-icon', Variables.imgPath + 'icons/icon_outlined.png', 'icon');
        }

        /**
         * initializes the overall page
         * @private
         */
        function _initMainPage(targetDom){
            _mainPage = Genny.createDiv(targetDom, 'main-page', 'container-absolute page main-page', true);
        }


        /**
         * initializes the UI
         * @private
         */
        function _initUI(targetDom){

            // create page
            _initMainPage(targetDom);

            // init all other UI modules
            MainContent.initMainContent(_mainPage);
            Menu.initMenu(_mainPage);

            // finally:  create page dimmer
            PageDimmer.initPageDimmer(targetDom);

        }

        /**
         * initializes all events
         * @private
         */
        function _initEvents(){

            // init menu events
            MenuEvents.initMenuEvents();

            // init page dimmer events
            PageDimmerEvents.registerPageDimmerEvents();

        }

        /**
         * Initializes the loading screen and makes it fade in.
         * Also loads the rest of the content and page.
         * @private
         */
        function initLoading(targetDom) {

            Loading.initLoadingScreen(targetDom);

            setTimeout(function(){

                Loading.showLoadingScreen();

                setTimeout(function() {

                    // init UI
                    _initUI(targetDom);
                    // init events
                    _initEvents();

                    // make the loading screen disappear
                    Loading.hideLoadingScreen();

                    // set focus to the page
                    PageDimmer.clarifyScreen();

                    // check for user session
                    UserEvents.checkForUserSession();

                },Loading.loadingFadeInTime);

            }, Loading.loadingFadeInDelay);

        }


        /**
         *  main entry point to init all modules
         */
        function _initApplication() {

            _initCSS();
            _initTitleAndIcon();

            _body = $("body") ;
            initLoading(_body);

        }


        /**
         * public interface
         */
        return {
            initApplication: _initApplication
        }

    });
